/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Entidad.Cliente;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
/**
 *
 * @author felipe and andres r
 * Using pair programming
 */
public class ClienteDAO {
    private static EntityManagerFactory emf=Persistence.createEntityManagerFactory("CRMPU");

    public boolean crear(Cliente object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean salida=false;
        try {
            em.persist(object);
            em.getTransaction().commit();
            salida = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return salida;
        }
    }
    
    public boolean eliminar(Cliente object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Cliente com = em.find(Cliente.class, object.getID());
        boolean ret = false;
        try {
            em.remove(com);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }
    
    public Cliente leer(double par) {

        EntityManager em = emf.createEntityManager();
        Cliente cliente = em.find(Cliente.class, par);
        return cliente;
    }
    
    public boolean actualizar(Cliente object, Cliente nuevoObjeto) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean ret = false;
        try {
            object.setID(nuevoObjeto.getID());
            object.setNombre(nuevoObjeto.getNombre());
            object.setEmail(nuevoObjeto.getEmail());
            object.setTelefono(nuevoObjeto.getTelefono());
            object.setDireccion(nuevoObjeto.getDireccion());
            em.merge(object);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
            }
        }
    
    public ArrayList<Double> listar (){
        ArrayList<Double> ids = new ArrayList<Double>();
        double l=0;
        EntityManager em = emf.createEntityManager();
        List lista = null;
        Query q = em.createQuery("SELECT c from Cliente c ");
        try {
            lista = q.getResultList();
            for (int i=0; i<lista.size(); i++){
                Cliente p = (Cliente)lista.get(i);
                //casting de double a int
                l = p.getID();
                ids.add(l);
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            return ids;
        }
    }
    
    public List<Cliente> bringAll(){
        EntityManager em = emf.createEntityManager();
        List<Cliente> cat = new ArrayList<Cliente>();
        
        Query q= em.createQuery("SELECT u FROM Cliente u");
        List<Cliente> tempCat = q.getResultList();
        try {
        for(Cliente c: tempCat){
            Cliente cate = new Cliente();
            cate.setNombre(c.getNombre());
            cate.setID(c.getID());
            cate.setEmail(c.getEmail());
            cate.setTelefono(c.getTelefono());
            cate.setDireccion(c.getDireccion());
            cat.add(cate);
        }
        
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            
            em.close();
            return cat;
        }
    }
    
}
